package com.hivekion.common.socket;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.util.*;

public class MyWebSocketVideosService extends WebSocketServer {

    public MyWebSocketVideosService(int port) {
        super(new InetSocketAddress(port));
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {

    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        Map<SocketReceiveVideosDataModel, WebSocket> mapList = SocketServerUtils.videosListMap;

        Set<Map.Entry<SocketReceiveVideosDataModel, WebSocket>> keySet = mapList.entrySet();
        Iterator<Map.Entry<SocketReceiveVideosDataModel, WebSocket>> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<SocketReceiveVideosDataModel, WebSocket> entry = iterator.next();
            SocketReceiveVideosDataModel dataModel = entry.getKey();
            WebSocket webSocket = entry.getValue();
            if (dataModel != null && StringUtils.isNoneBlank(dataModel.getTerminalId()) && webSocket != null) {
                if (webSocket.equals(conn)) {
                    SocketServerUtils.videosListMap.remove(dataModel);
                }
            }
        }
    }

    @Override
    public void onMessage(WebSocket socket, String message) {
        Map<SocketReceiveVideosDataModel, WebSocket> mapList = SocketServerUtils.videosListMap;
        if (message.contains("type") && message.contains("data")) {
            SocketReceiveVideosModel receiveVideosModel = JSON.parseObject(message, SocketReceiveVideosModel.class);
            SocketReceiveVideosDataModel receiveVideosDataModel = receiveVideosModel.getData();
            if (receiveVideosModel != null && receiveVideosDataModel != null) {
                if ("connect".equals(receiveVideosModel.getType())) {  //建立连接
                    if (!mapList.containsKey(receiveVideosDataModel)) {
                        SocketServerUtils.videosListMap.put(receiveVideosDataModel, socket);
                    }
                }
                if ("sourceAuthorize".equals(receiveVideosModel.getType())  //信号源授权
                        || "authorizeSuccess".equals(receiveVideosModel.getType()) //接收授权
                        || "authorizeFail".equals(receiveVideosModel.getType()) //拒绝授权
                        || "view".equals(receiveVideosModel.getType())   //信号源观看
                        || "viewSuccess".equals(receiveVideosModel.getType())  //观看
                        || "call".equals(receiveVideosModel.getType())        //通讯录呼叫
                        || "callFail".equals(receiveVideosModel.getType())    //呼叫拒绝
                        || "callSuccess".equals(receiveVideosModel.getType())  //呼叫接听
                        || "callOff".equals(receiveVideosModel.getType())      //挂断
                        || "mute".equals(receiveVideosModel.getType())      //静音
                        || "muteRelieve".equals(receiveVideosModel.getType())      //解除静音
                        || "custom".equals(receiveVideosModel.getType())         //自定义
                        || "LineHandwriting".equals(receiveVideosModel.getType())                //画笔笔迹
                        || "LineHandwritingStart".equals(receiveVideosModel.getType())                //画笔笔迹
                        || "LineHandwritingEnd".equals(receiveVideosModel.getType())                //画笔笔迹
                        || "CAMERA_COM".equals(receiveVideosModel.getType())                //设备控制
                        || "CAMERA_IP".equals(receiveVideosModel.getType())                //设备控制
                        || "Operation_Bed".equals(receiveVideosModel.getType())                //设备控制
                        || "SRG_301SE".equals(receiveVideosModel.getType())                //设备控制--SRG_301SE
                ) {
                    Set<Map.Entry<SocketReceiveVideosDataModel, WebSocket>> keySet = mapList.entrySet();
                    Iterator<Map.Entry<SocketReceiveVideosDataModel, WebSocket>> iterator = keySet.iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<SocketReceiveVideosDataModel, WebSocket> entry = iterator.next();
                        SocketReceiveVideosDataModel dataModel = entry.getKey();
                        WebSocket webSocket = entry.getValue();
                        if (receiveVideosDataModel.getToTerminalId().contains(dataModel.getTerminalId())) {
                            webSocket.send(message);
                        }
                    }
                }
            }
        }
        System.out.println("收到消息：" + message);
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {

    }

    @Override
    public void onStart() {

    }

}
